package UseFile;

public class BisectionMethod {
    public static int binarySearch(int[] nums,int v,int start,int end){
        if(start>end){
            return -1;
        }
        int mid = start+(end-start)/2;
        if (nums[mid]==v){
            return mid;
        } else if (v<nums[mid]) {
            return binarySearch(nums,v,start,mid-1);
        }else {
            return binarySearch(nums,v,mid+1,end);
        }
    }

    public static void main(String[] args) {
        int[] nums={1,2,4,5,6,8,9,12,23,45,67,68,79,80,90,200};
        int target1 = 23;
        int result1 = binarySearch(nums,target1,0,nums.length-1);
        System.out.println("查找"+target1+":"+(result1!=-1 ? "找到，索引为"+result1:"未找到"));

        int target2 = 7;
        int result2 = binarySearch(nums,target2,0,nums.length-1);
        System.out.println("查找"+target2+":"+(result2!=-1 ? "找到，索引为"+result2:"未找到"));

        int target3 = 1;
        int result3 = binarySearch(nums,target3,0,nums.length-1);
        System.out.println("查找"+target3+":"+(result3!=-1 ? "找到，索引为"+result3:"未找到"));

        int target4 = 200;
        int result4 = binarySearch(nums,target4,0,nums.length-1);
        System.out.println("查找"+target4+":"+(result4!=-1 ? "找到，索引为"+result4:"未找到"));
    }
}
